﻿package mortal.game.view.friend
{
    import Message.Game.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import extend.language.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import mortal.common.global.*;
    import mortal.common.net.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.mvc.core.*;

    public class FriendDeletePanel extends SmallWindow
    {
        private var _centerBg:ScaleBitmap;
        private var _leftBg:ScaleBitmap;
        private var _rightBg:ScaleBitmap;
        private var _leftBar:ScaleBitmap;
        private var _rightBar:ScaleBitmap;
        private var _selectedDelete:GTextFiled;
        private var _selectNum:GTextFiled;
        private var _closeVlue:GTextFiled;
        private var _confirmDelete:GTextFiled;
        private var _deleteNum:GTextFiled;
        private var _selectedList:GTileList;
        private var _deleteList:GTileList;
        private var _confirmBtn:GButton;
        private var _cancelBtn:GButton;
        private var _currFriendNum:int;
        private var _currOnlineNum:int;
        private var _selectedNum:int;

        public function FriendDeletePanel()
        {
            this.init();
            this.layer = LayerManager.windowLayer;
            return;
        }// end function

        private function init() : void
        {
            setSize(521 - 13, 367);
            title = Language.getString(41219);
            this.isHideDispose = false;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._leftBg = UIFactory.bg(13 - 5, 30 + 7, 330, 312 - 25, this);
            this._rightBg = UIFactory.bg(345 - 5, 30 + 7, 172, 312 - 25, this);
            this._leftBar = UIFactory.bg(15 - 5, 32 + 7, 325, 28, this, ImagesConst.RegionTitleBg);
            this._rightBar = UIFactory.bg(347 - 5, 32 + 7, 168, 28, this, ImagesConst.RegionTitleBg);
            this._selectedDelete = UIFactory.gTextField(Language.getString(41220), 47 + 17, 38 + 4, 92, 20, this, GlobalStyle.textFormatBai);
            this._selectNum = UIFactory.gTextField("", 140 - 5, 38 + 4, 60, 20, this, GlobalStyle.textFormatBai);
            this._closeVlue = UIFactory.gTextField(Language.getString(41221), 260 - 5, 38 + 4, 48, 20, this, GlobalStyle.textFormatBai);
            this._confirmDelete = UIFactory.gTextField(Language.getString(41222), 398 - 5, 38 + 4, 57, 20, this, GlobalStyle.textFormatBai);
            this._deleteNum = UIFactory.gTextField("", 450 - 5, 38 + 4, 35, 20, this, GlobalStyle.textFormatBai);
            this.updateRoleNum();
            this._selectedList = UIFactory.tileList(10 - 5, 60 + 7, 331, 280 - 25, this);
            this._selectedList.columnWidth = 331 - 16;
            this._selectedList.rowHeight = 25;
            this._selectedList.horizontalGap = 0;
            this._selectedList.verticalGap = 0;
            this._selectedList.setStyle("skin", new Bitmap());
            this._selectedList.setStyle("cellRenderer", SelectedCellRenderer);
            this.addChild(this._selectedList);
            this._deleteList = UIFactory.tileList(342 - 5, 60 + 7, 171, 280 - 25, this);
            this._deleteList.columnWidth = 168;
            this._deleteList.rowHeight = 25;
            this._deleteList.horizontalGap = 0;
            this._deleteList.verticalGap = 0;
            this._deleteList.setStyle("skin", new Bitmap());
            this._deleteList.setStyle("cellRenderer", DeleteCellRenderer);
            this.addChild(this._deleteList);
            this._confirmBtn = UIFactory.gButton(Language.getString(41223), 167 - 5, 346 - 11, 77, 23, this);
            this._cancelBtn = UIFactory.gButton(Language.getString(41224), 278 - 5, 346 - 11, 77, 23, this);
            this._confirmBtn.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._cancelBtn.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._selectedList.configEventListener(MouseEvent.CLICK, this.onListClickHandler);
            return;
        }// end function

        override protected function setWindowCenter() : void
        {
            return;
        }// end function

        override protected function configParams() : void
        {
            super.configParams();
            return;
        }// end function

        private function onClickHandler(event:MouseEvent) : void
        {
            var len:uint;
            var ids:Array;
            var i:uint;
            var data:Object;
            var friendInfo:SelectedCellRenderer;
            var e:* = event;
            len = this._deleteList.dataProvider.length;
            if (e.target == this._confirmBtn)
            {
                ids;
                if (len > 0)
                {
                    var onClickHandler:* = function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    i = 0;
                    while (i < len)
                    {
                        
                        friendInfo = _deleteList.dataProvider.getItemAt(i).data as SelectedCellRenderer;
                        friendInfo.isSelected = false;
                        var _loc_3:* = i + 1;
                        i = _loc_3;
                    }
                    Dispatcher.dispatchEvent(new DataEvent(EventName.FriendDelete, data));
                }
                else
                {
                    return;
                }
                return;
            }// end function
            ;
                    i;
                    while (i < len)
                    {
                        
                        friendInfo = this._deleteList.dataProvider.getItemAt(i).data as SelectedCellRenderer;
                        ids.push(friendInfo.recordId);
                        i = (i + 1);
                    }
                    data;
                    data["ids"] = ids;
                    data["type"] = 1;
                    Alert.show(Language.getString(41225), null, Alert.OK | Alert.CANCEL, null, onClickHandler);
                }
            }
            else if (len > 0)
            {
                i;
                while (i < len)
                {
                    
                    friendInfo = this._deleteList.dataProvider.getItemAt(i).data as SelectedCellRenderer;
                    friendInfo.isSelected = false;
                    i = (i + 1);
                }
                this._deleteList.dataProvider = new DataProvider();
                this._selectedNum = 0;
                this._deleteNum.text = "[" + this._selectedNum + "]";
            }
            return;
        }// end function

        public function updateList(param1:Object) : void
        {
            var _loc_2:DataProvider = null;
            var _loc_3:Array = null;
            var _loc_4:SFriendRecord = null;
            var _loc_5:Object = null;
            var _loc_6:SelectedCellRenderer = null;
            var _loc_7:Object = null;
            if (param1 is Array)
            {
                _loc_2 = new DataProvider();
                _loc_3 = param1 as Array;
                for each (_loc_4 in _loc_3)
                {
                    
                    _loc_5 = {};
                    _loc_5.data = _loc_4;
                    _loc_2.addItem(_loc_5);
                }
                this._selectedList.dataProvider = _loc_2;
                this._selectedList.drawNow();
            }
            if (param1 is SelectedCellRenderer)
            {
                _loc_6 = param1 as SelectedCellRenderer;
                _loc_7 = {};
                _loc_7.data = _loc_6;
                this._deleteList.dataProvider.addItem(_loc_7);
                this._deleteList.drawNow();
            }
            this.updateRoleNum();
            return;
        }// end function

        private function onListClickHandler(event:MouseEvent) : void
        {
            CallLater.addCallBack(this.nextFramWhenClickDeleteList);
            return;
        }// end function

        private function nextFramWhenClickDeleteList() : void
        {
            var _loc_1:* = this._selectedList.itemToCellRenderer(this._selectedList.selectedItem) as SelectedCellRenderer;
            if (_loc_1 && !_loc_1.isSelected)
            {
                _loc_1.isSelected = true;
                Cache.instance.friend.updateDeleteDic(_loc_1.recordId, true);
                Dispatcher.dispatchEvent(new DataEvent(EventName.FriendAddToDelete, _loc_1));
                var _loc_2:String = this;
                var _loc_3:* = this._selectedNum + 1;
                _loc_2._selectedNum = _loc_3;
                this._deleteNum.text = "[" + this._selectedNum + "]";
            }
            return;
        }// end function

        public function cancelDelete(param1:Object) : void
        {
            if (param1 != null)
            {
                this._deleteList.dataProvider.removeItem(param1);
                (param1.data as SelectedCellRenderer).isSelected = false;
                Cache.instance.friend.updateDeleteDic((param1.data as SelectedCellRenderer).recordId, false);
                var _loc_2:String = this;
                var _loc_3:* = this._selectedNum - 1;
                _loc_2._selectedNum = _loc_3;
                this._deleteNum.text = "[" + this._selectedNum + "]";
            }
            return;
        }// end function

        public function clear() : void
        {
            var _loc_2:SelectedCellRenderer = null;
            this._deleteList.dataProvider = new DataProvider();
            var _loc_1:* = this._selectedList.dataProvider.length;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_1)
            {
                
                _loc_2 = this._selectedList.itemToCellRenderer(this._selectedList.getItemAt(_loc_3)) as SelectedCellRenderer;
                if (_loc_2 && _loc_2.isSelected)
                {
                    _loc_2.isSelected = false;
                }
                _loc_3++;
            }
            this._selectedNum = 0;
            this._deleteNum.text = "[" + this._selectedNum + "]";
            Cache.instance.friend.clearDeleteList();
            return;
        }// end function

        public function updateRoleNum() : void
        {
            this._currFriendNum = Cache.instance.friend.friendList.length;
            this._currOnlineNum = Cache.instance.friend.getCurrOnlineNum(0);
            this._selectNum.text = "[" + this._currOnlineNum + "/" + this._currFriendNum + "]";
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._leftBg.dispose(param1);
            this._rightBg.dispose(param1);
            this._leftBar.dispose(param1);
            this._rightBar.dispose(param1);
            this._selectedDelete.dispose(param1);
            this._selectNum.dispose(param1);
            this._closeVlue.dispose(param1);
            this._confirmDelete.dispose(param1);
            this._deleteNum.dispose(param1);
            this._selectedList.dispose(param1);
            this._deleteList.dispose(param1);
            this._confirmBtn.dispose(param1);
            this._cancelBtn.dispose(param1);
            this._leftBg = null;
            this._rightBg = null;
            this._leftBar = null;
            this._rightBar = null;
            this._selectedDelete = null;
            this._selectNum = null;
            this._closeVlue = null;
            this._confirmDelete = null;
            this._deleteNum = null;
            this._selectedList = null;
            this._deleteList = null;
            this._confirmBtn = null;
            this._cancelBtn = null;
            Cache.instance.friend.clearDeleteList();
            return;
        }// end function

    }
}
